package cn.tanghom.app.web.controller;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/process")
public class ActivitiController {
	private static Logger logger = Logger.getLogger(ActivitiController.class);

	@Autowired
	ProcessEngineFactoryBean processEngine;
	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;

	@Autowired
	private HistoryService historyService;

	@Autowired
	private IdentityService identityService;

	@RequestMapping(value = "test")
	@ResponseBody
	public String test() {
		repositoryService.createDeployment().addClasspathResource("cn/tanghom/app/web/controller/test.bpmn20.xml").deploy();
		System.out.println("Number of process definitions: " + repositoryService.createProcessDefinitionQuery().count());

		identityService.setAuthenticatedUserId("Jeff Dean");

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
		System.out.println(processInstance.getId());
		System.out.println(processInstance.getProcessDefinitionId());
		System.out.println(processInstance.getProcessDefinitionKey());

		List<Task> tasks = taskService.createTaskQuery().taskAssignee("kermit").list();
		for (Task task : tasks) {
			System.out.println(task.getName() + " : " + task.getAssignee());

			taskService.claim(task.getId(), "kermit");
		}

		tasks = taskService.createTaskQuery().taskAssignee("kermit").list();
		for (Task task : tasks) {

			taskService.complete(task.getId());
			System.out.println(task.getName() + " : " + task.getId() + " completed ");
		}

		try {
			Thread.sleep(10 * 1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		tasks = taskService.createTaskQuery().taskAssignee("stone").list();
		for (Task task : tasks) {
			System.out.println(task.getName() + " : " + task.getAssignee());
			taskService.claim(task.getId(), "stone");
		}

		tasks = taskService.createTaskQuery().taskAssignee("stone").list();
		for (Task task : tasks) {
			taskService.complete(task.getId());
			System.out.println(task.getName() + " : " + task.getId() + " completed ");
		}

		tasks = taskService.createTaskQuery().taskAssignee("kermit").list();
		for (Task task : tasks) {
			System.out.println(task.getName() + " : " + task.getAssignee());
			taskService.claim(task.getId(), "kermit");
		}

		HistoricProcessInstance hpInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
		System.out.println("end time: " + hpInstance.getEndTime());
		return "qidong";
	}
}